home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / gag / HAMmmm2.lha / JGoodies / HAMmmm2 / mmm_draw < prev    next >
Text File  |  1989-08-21  |  5KB  |  231 lines

  1. \ HAM drawing routine for HAMmmm.
  2. \ Provides several points which bounce around on screen.
  3. \ Lines are then drawn beteen pairs of points.
  4. \
  5. \ Author: Phil Burk
  6. \ Copyright 1987 Phil Burk
  7. \ This code is considered to be in the public domain and
  8. \ may be freely distributed but may not be sold for profit.
  9.  
  10. ANEW TASK-MMM_DRAW
  11.  
  12. \ Variables used to control modes of HAM drawing.
  13. variable HAM-DRAW-MODE
  14.  
  15. 0  dup constant HAM_DRAW_BETWEEN
  16. 1+ dup constant HAM_DRAW_STAR
  17. 1+ dup constant HAM_DRAW_BOXES
  18. 1+ constant HAM_MANY_MODES
  19.  
  20.  
  21. ham_xmax ham_xmin - 16 / constant HAM_X/16
  22.  
  23. : HAM.SHOW.COLORS  ( -- , test routine )
  24.     16 0
  25.     DO i gr.color!
  26.        ham_xmin ham_x/16 i * + ham_ymin
  27.        over ham_x/16 + ham_ymax gr.rect
  28.     LOOP
  29. ;
  30.  
  31. : HAM.SET.RGB ( n red green blue -- )
  32.     >r >r >r >r
  33.     hamscreen @ .. sc_viewport >abs
  34.     r> r> r> r>
  35.     call graphics_lib setrgb4 drop
  36. ;
  37.  
  38. : HAM.SET.COLORS  ( -- , create a rainbow like spectrum )
  39.     0 0 0 0 ham.set.rgb  ( set zero to black )
  40.     8 0  ( red to green )
  41.     DO i 1+  ( index )
  42.        15  i 15 * 8 / -   ( red )
  43.        i 15 * 8 /         ( green )
  44.        0  ( blue )
  45.        ham.set.rgb
  46.     LOOP
  47.     8 0  ( green to blue )
  48.     DO i 8 +
  49.        0  ( red )
  50.        15  i 15 * 8 / -   ( green )
  51.        i 15 * 8 /         ( blue )
  52.        ham.set.rgb
  53.     LOOP
  54. ;     
  55.        
  56. \ Constants for building colors.
  57. $ 10 constant HAM_CHANGE_BLUE
  58. $ 20 constant HAM_CHANGE_RED
  59. $ 30 constant HAM_CHANGE_GREEN
  60.  
  61. : HAM.COLOR!  ( RGB new_value -- , set HAM color )
  62.     OR gr.color!
  63. ;
  64.  
  65. ham_ymax ham_ymin  + 2/ constant HAM_1/2Y
  66.  
  67. : HAM.FILL.SCREEN ( -- , split into Redless top and Blueless bottom )
  68.     ham_change_red 0 ham.color!
  69.     ham_xmin ham_ymin ham_xmax ham_1/2y gr.rect
  70.     ham_change_blue 0 ham.color!
  71.     ham_xmin ham_1/2y ham_xmax ham_ymax gr.rect
  72. ;
  73.  
  74. 10 constant HAM_NUM_LINES
  75. ham_num_lines 2* constant HAM_NUM_POINTS
  76.  
  77. ham_num_points array HAM-X-POS
  78. ham_num_points array HAM-Y-POS
  79. ham_num_points array HAM-X-VEL
  80. ham_num_points array HAM-Y-VEL
  81.  
  82. \ ------------------------------------------------------
  83. : HAM.GET.RECT ( index -- x1 y1 x2 y2 , get unsorted corners )
  84.     >r
  85.     r@ ham-x-pos @ r@ ham-y-pos @
  86.     r@ ham_num_lines + ham-x-pos @
  87.     r@ ham_num_lines + ham-y-pos @
  88.     rdrop
  89. ;
  90. : HAM.DRAW.LINE  ( index -- , draw line between points)
  91.     ham.get.rect
  92.     gr.move gr.draw
  93. ;
  94.  
  95. ham_ymin ham_ymax + constant ham_ysum
  96. : REFLECT.Y  ( y -- y' )
  97.     ham_ysum swap -
  98. ;
  99.  
  100. : HAM.DRAW.BETWEEN ( index -- )
  101.     dup ham.draw.line
  102.     ham.get.rect
  103.     reflect.y >r >r
  104.     reflect.y r> r>   ( reflect about y center )
  105.     gr.move gr.draw
  106. ;
  107.  
  108. : HAM.START.POINT ( -- x y )
  109.     0 ham-x-pos @
  110.     0 ham-y-pos @
  111. ;
  112.  
  113. : HAM.DRAW.STAR ( index -- , draw from one point )
  114.     ham.get.rect
  115.     ham.start.point gr.move gr.draw
  116.     ham.start.point gr.move gr.draw
  117. ;
  118.  
  119. : DRAW.BOX { x1 y1 x2 y2 -- }
  120.     x1 y1 gr.move
  121.     x2 y1 gr.draw x2 y2 gr.draw
  122.     x1 y2 gr.draw x1 y1 gr.draw
  123. ;
  124.  
  125. : HAM.DRAW.BOXES ( index -- , draw box )
  126.     ham.get.rect
  127.     draw.box
  128. ;
  129.  
  130. : HAM.DRAW.POINTS  ( index -- , draw based on current mode selections )
  131.     ham-draw-mode @
  132.     CASE
  133.         ham_draw_between
  134.         OF ham.draw.between
  135.         ENDOF
  136. \
  137.         ham_draw_star
  138.         OF ham.draw.star
  139.         ENDOF
  140. \
  141.         ham_draw_boxes
  142.         OF ham.draw.boxes
  143.         ENDOF
  144. \
  145.         ham.draw.between   ( default )
  146.     ENDCASE
  147. ;
  148.     
  149. : HAM.MOVE.DIM  { position velocity minpos maxpos -- }
  150.     position @ velocity @ + dup minpos maxpos within?
  151.     IF position !
  152.     ELSE drop velocity @ negate velocity !
  153.     THEN
  154. ;
  155.  
  156. : HAM.MOVE.POINT  ( index -- )
  157.     >r
  158.     r@ ham-x-pos r@ ham-x-vel ham_ymin ham_xmax ham.move.dim
  159.     r@ ham-y-pos r@ ham-y-vel ham_ymin ham_ymax ham.move.dim
  160.     rdrop
  161. ;
  162.  
  163. : HAM.MOVE.LINE  ( -- )
  164.     dup ham.move.point
  165.     ham_num_lines + ham.move.point
  166. ;
  167.  
  168. : HAM.SETUP.POS  ( -- , choose initial random cluster )
  169.     ham_xmax 60 -  ham_ymax 2/
  170.     ham_num_points 0
  171.     DO 2dup 20 choose + i ham-y-pos !
  172.        20 choose + i ham-x-pos !
  173.     LOOP 2drop
  174. ;
  175.  
  176. : CHOOSE.NONZERO ( -- num , return nonzero random number )
  177.     16 choose 7 - dup 1 <
  178.     IF 1- THEN
  179. ;
  180.  
  181. : HAM.SETUP.VEL ( -- , choose random velocities )
  182.     ham_num_points 0
  183.     DO choose.nonzero i ham-y-vel !
  184.        choose.nonzero i ham-x-vel !
  185.     LOOP
  186. ;
  187.  
  188. VARIABLE HAM-COLOR-OFFSET
  189.  
  190. : HAM.CHANGE.MODE ( -- )
  191.     ham_many_modes choose ham-draw-mode !
  192. ;
  193.  
  194. : HAM.MOVING.LINES ( -- , draw one complete pass )
  195.     swap.buffers     ( next surface )
  196.     ham.fill.screen  ( clear screen )
  197. \
  198. \ Move all points.
  199.     ham_num_points 0  
  200.     DO i ham.move.point
  201.     LOOP
  202. \
  203. \ Draw lines between them.
  204.     ham-color-offset @ -4 ashift
  205.     ham_num_lines 0
  206.     DO  i 3 * over + 63 and
  207.         gr.color!  ( move through rgb spectrum )
  208.         i ham.draw.points
  209.     LOOP drop
  210. \
  211. \ Make visible.
  212.     ham.rebuild
  213.     1 ham-color-offset +!
  214. ;
  215.  
  216. : HAM.DUMP.POS ( -- , for debugging )
  217.     cr ham_num_points 0
  218.     DO i . i ham-x-pos @ .
  219.        i ham-y-pos @ . cr
  220.     LOOP
  221. ;
  222.  
  223. : HAM.DUMP.VEL ( -- , for debugging )
  224.     cr ham_num_lines 0
  225.     DO i .
  226.        i ham-x-vel @ .
  227.        i ham-y-vel @ . cr
  228.     LOOP
  229. ;
  230.  
  231.